/**
* @description : Pdf预览
* @descriptionDetail : Pdf预览
* @copyright : 浙江烟草
* @author : mc
* @create : 2022-12-05 16:22:51
*/
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
      <view class="nav-right-icon">
        <u-icon name="download" color="#FFF" @tap="onPrint"></u-icon>
        <!-- 此功能仅针对部分文书开放  废弃
        <u-icon v-if="isSign" name="share" color="#FFF" @tap="onShare" style="margin-left: 8px;"></u-icon>-->
      </view>
    </theme-navbar>
    <!-- #endif -->
    <!-- #ifdef H5-WECHAT || H5-EMP -->
    <fab-button :content="fContent" @fabtrigger="fabtrigger" />
    <!-- #endif -->
    <view class="content u-page pdf-container">
      <web-view :src="pdfSrc"></web-view>
    </view>
    <u-popup v-model="qrcodeShow" mode="bottom" height="70%" @close="qrcodeShow=false">
      <view class="qrcodeContainer">
        <img :src="qrcodeImg" alt="暂无图片" />
        <p>请扫码签名</p>
      </view>
    </u-popup>
  </view>
</template>
<script>
import {
  PATHROOT,
} from '@/common/path.config.js';
import QRCode from 'qrcode';
import fabButton from '@/components/uni-fab.vue';
import commonService from "@/service/common/common.service.js";
import {
	DD_MODEL
} from "@/config/setting.config.js";
export default {
  components: {
    fabButton
  },
  onLoad () {
    let param = this.getRouteParam();
    this.fileId = param.fileId; // 文件id
    this.fileName = param.fileName; // 文件名 可不传
    this.isSign = param.isSign; // 是否可以第三方签名
    this.url = param.url;
    this.isDownload = typeof param.isDownload === 'undefined' ? true : param.isDownload; // 是否提供下载按钮
  },
  data () {
    return {
      url:"", // 替换url   仓库的文书pdf请求url不一样
      fileUrl: PATHROOT.BASE + "/commonfile/filestore/downloadFilePdf.do",
      userInfo: this.$storage.get(this.$rp.common.USER),
      fileId: "",
      fileName: "",
      isSign: false,
      isDownload: true,
      pdfSrc: "",
      isIos: false,
      qrcodeShow: false, // 二维码是否显示
      qrcodeImg: "",
      fContent: [{
        iconPath: require('@/static/down.png'),
        selectedIconPath: require('@/static/down.png'),
        text: '下载',
        active: false,
      }],
    };
  },
  mounted () {
    this.setRightBtns();
    if (this.fileName) {
      this.navTitle = this.fileName;
    }
    let u = navigator.userAgent;
    this.isIos = (!!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/));
    // if(this.isIos){
    //   this.fileUrl = PATHROOT.BASE + "/commonfile/filestore/downloadFilePdf.do";
    // }else{
    this.fileUrl = PATHROOT.BASE + "/commonfile/filestore/downloadFile.do";
    // }
    this.init();
  },
  methods: {
    setRightBtns () {
      // 设置右侧标题
      // #ifdef H5-DINGDING
      let self = this;
      if (this.isDownload) {
        this.$deviceApi.setRight("下载", () => {
          self.onPrint();
          self.setRightBtns();
        }, true, true);
      }
      // #endif
    },
    fabtrigger (btn) {
      if (btn.index == 0) {
        this.onPrint();
      }
    },
    init () {
      if (this.url) {
        this.fileUrl = this.url;
      }
      this.pdfSrc = location.origin + location.pathname + "/pdf/web/viewer.html?file=" + encodeURIComponent(this.genericPath());
    },
    /**
     * 生成预览路径
     * @returns {string}
     */
    genericPath () {
      return this.fileUrl + "?tk=" + this.userInfo.tk + "&fileId=" + this.fileId;
    },
    onPrint() {
      // #ifdef H5-DINGDING || H5-ONLY
      if (this.isIos && this.$deviceApi.shareContent) {
        this.$modal.alert("苹果不支持下载,点击确定后请选择'默认浏览器打开',在默认浏览器中点击底部菜单选择打印", "提示", () => {
          this.$deviceApi.shareContent(0, "", this.genericPath());
        });
      } else {
        this.$modal.downloadFile(this.genericPath(), Date.now() + ".pdf");
      }
      // #endif
      // #ifdef H5-WECHAT
      this.$u.loading('正在获取文件信息...');
      commonService.findFileByFileId(this.fileId).then(res => {
        this.$u.hideLoading();
        console.log(res)
        if(res.code == 200 && res.success) {
          let gfiSize = res.bean.gfiSize;
          if (DD_MODEL === 1) {
            console.log('----gfiStoreName----')
            this.$deviceApi.printWeChat(this.genericPath(), res.bean.gfiStoreName, gfiSize)
          } else {
            if(this.isIos) {
              gfiSize = 0;
            }
            this.$modal.downloadFile(this.genericPath(), Date.now() + ".pdf",gfiSize);
          }
        } else {
          this.$u.toast("获取文件信息失败！");
        }
      }).catch((err) => {
        this.$u.toast(err);
        this.$u.hideLoading();
      })
      // #endif
    },
    // 生成一个二维码分享给当事人签名
    onShare5 () {
      if (this.qrcodeImg) {
        this.qrcodeShow = true;
        return;
      }
      this.$u.loading('正在生成二维码...');
      // 此处生产的分享链接借用当前登录人的tk，用于获取基础数据
      // TODO 待定是否设置有效期（十分钟后过期）,为了安全考虑，需增加一个校验
      let expireTime = new Date().getTime() + 10 * 60 * 1000;
      let qrcodeUrl = location.origin + location.pathname + `#/pages/commonPage/signDoc?fileId=${this.fileId}&un=${this.$storage.get(this.$rp.common.USER).loginName}&et=${expireTime}`;
      // let qrcodeUrl = "http://10.44.7.2:8080/zmyd/#/pages/commonPage/signDoc?fileId=" + this.fileId;
      console.log("二维码地址", qrcodeUrl);
      QRCode.toDataURL(qrcodeUrl).then(path => {
        this.qrcodeImg = path;
        this.qrcodeShow = true;
        this.$u.hideLoading();
      }).catch(err => {
        this.$u.toast(err);
        this.$u.hideLoading();
      });
    },
    onShare () {
      // if (this.qrcodeImg) {
      //   this.qrcodeShow = true;
      //   return;
      // }
      // this.$u.loading('正在生成二维码...');
      // // 此处生产的分享链接借用当前登录人的tk，用于获取基础数据
      // commonService.dsrSign();
      // // TODO 待定是否设置有效期（十分钟后过期）,为了安全考虑，需增加一个校验
      // let qrcodeUrl = location.origin + location.pathname + `#/pages/commonPage/signDoc?fileId=${this.fileId}&un=${this.$storage.get(this.$rp.common.USER).loginName}&et=${expireTime}`;
      // console.log("二维码地址", qrcodeUrl);
      // // QRCode.toDataURL(qrcodeUrl).then(path => {
      // //   this.qrcodeImg = path;
      // //   this.qrcodeShow = true;
      // //   this.$u.hideLoading();
      // // }).catch(err => {
      // //   this.$u.toast(err);
      // //   this.$u.hideLoading();
      // // });
    },
  },
};
</script>
<style lang="scss" scoped>
	@import "@/style/common/rightMenu.scss";
	.pdf-container {
		height: 100%;
		width: 100%;
	}

	iframe {
		height: 100%;
		min-height: 480px;
		width: 100%;
	}
  .qrcodeContainer {
    text-align: center;
    img {
      width: 95%;
    }
    p {
      font-size: 16px;
      font-weight: 600;
    }
  }
</style>

